package com.wm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.wm.bean.Dish;
import com.wm.bean.Restaurant;
import com.wm.dao.DishDao;
import com.wm.dao.RestaurantDao;
import com.wm.service.SearchService;
import com.wm.vo.DishListVO;
import com.wm.vo.RestaurantListVO;

public class SearchServiceImpl implements SearchService {
	@Resource private RestaurantDao restaurantDao;
	@Resource private DishDao dishDao;
	
	public RestaurantListVO SearchRest(RestaurantListVO restaurantListVO) {
		
			int maxResults = 10;
			int beginNum = (restaurantListVO.requestPage-1)*maxResults;
	//System.out.println("beginNum="+beginNum);
			
			String hql ="from Restaurant r where r.uid ='"+restaurantListVO.uid+"'";
		/*	for (int i = 0; i < restaurantListVO.getRestaurantsList().size(); i++) {
				hql +=  " or Restaurant.name like '%" + restaurantListVO.getRestaurantsList().get(i) + "%' "; 
			}*/
			
			if (!restaurantListVO.keyWord.equals("")) {
				hql += "and r.name like '%" + restaurantListVO.keyWord + "%'";
			}
System.out.println(hql);
			restaurantListVO.totalPage = restaurantDao.getRestPagesWithHql(maxResults, hql);
			if (restaurantListVO.requestPage > restaurantListVO.totalPage ||restaurantListVO.requestPage < 0) {
				beginNum = 0;
				restaurantListVO.requestPage = 1;
			}
//			System.out.println("beginNum="+beginNum);
			restaurantListVO.restaurantsList = restaurantDao.getRestaurantsByNameHql(beginNum,maxResults,hql);
			return restaurantListVO;

	}

	public DishListVO searchDish(DishListVO dishListVO) {
		
		int maxResults = 10;
		int beginNum = (dishListVO.requestPage-1)*maxResults;
//System.out.println("beginNum="+beginNum);
		
		String hql ="from Dish d where d.rest.uid = '"+dishListVO.uid+"'";
		if (!dishListVO.keyWord.equals("")) {
			hql += "and d.name like '" + dishListVO.keyWord + "'";
		}
System.out.println(hql);
		dishListVO.totalPage = dishDao.getDishPagesWithHql(maxResults, hql);
		if (dishListVO.requestPage > dishListVO.totalPage ||dishListVO.requestPage < 0) {
			beginNum = 0;
			dishListVO.requestPage = 1;
		}
//		System.out.println("beginNum="+beginNum);
		dishListVO.dishList = dishDao.getDishsByNameHql(beginNum,maxResults,hql);
		return dishListVO;
		
		
		
		
		
		
		
		/*System.out.println(dish);
		String hql="";
		if(!dish.getName().equals(null)||!dish.getName().equals("")){
			hql="from Dish d where d.name like'%"+dish.getName()+"%'";
		}else{
			hql="from Dish d ";
		}
		
		DishListVO dishListVO = new DishListVO();
		dishListVO.dishList = dishDao.getDishByName(hql);
		return dishListVO;
	}

	public List<Restaurant> searchShop(Restaurant restaurant) {
		// TODO Auto-generated method stub
		String hql ="from Restaurant r where rid = '"+ restaurant.getRid()+"'";
		
		return restaurantDao.searchShop(hql);*/
	}

	public List<Restaurant> searchShop(Restaurant restaurant) {
		// TODO Auto-generated method stub
		return null;
	}


}
